<? 
	include('base.php');
	User::protect();
	include_class('general_media');
	include_class('m2');
	$id = ($_GET['id'] > 0 && is_numeric($_GET['id'])) ? $_GET['id'] : 0;
	$section = 'media';

	$ma = MediaArea::get($id);
	$gm = new GeneralMedia();
	$gm->setAreaObject($ma);

	if (!db::isError($ma)) { 
		switch($_GET['task']) {
			case 'rescan':
				if ($ma) {
					$res = $ma->rescan($gm);
					if (!db::isError($res)) { 
						header('Location: media.php?id=' . $_GET['id']);
						exit;
					}
				}
				break;
			case 'move_up':
				if ($ma) {
					$mi = MediaInstance::get($_GET['media_instance_id']);
					if (!db::isError($mi)) {
						$res = $mi->moveUp($gm);
						if (!db::isError($res)) { 
							header('Location: media.php?id=' . $_GET['id']);
							exit;
						}
					}
				}
				break;
			case 'move_down':
				if ($ma) {
					$mi = MediaInstance::get($_GET['media_instance_id']);
					if ($mi) {
						$res = $mi->moveDown($gm);
						if (!db::isError($res)) { 
							header('Location: media.php?id=' . $_GET['id']);
							exit;
						}
					}
				}
				break;
			case 'activate':
				if ($ma) {
					$res = $ma->activate();
					if (!db::isError($res)) { 
						header('Location: media.php?id=' . $_GET['id']);
						exit;
					}
				}
				break;
			case 'deactivate':
				if ($ma) {
					$res = $ma->deactivate();
					if (!db::isError($res)) { 
						header('Location: media.php?id=' . $_GET['id']);
						exit;
					}
				}
				break;
			case 'delete':
				if ($ma) {
					$res = $ma->delete();
					$parentID = $ma->getParentID();
					
					if (!db::isError($res)) {
						header('Location: media.php?id=' . $parentID);
						exit;
					}
				}
				break;
		}
	}
	
	$page_title = 'Media';
	include('layout/header.php');

?>

<?	if (!db::isError($ma)) { 
	
	if ($ma->isGeneralMedia()) { ?>

		<div id="breadcrumb">
			<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="media.php">Media&nbsp;&#62;</a>
			<? $breadcrumb = $ma->getAreaTrail();
				if ($breadcrumb) { 
					foreach($breadcrumb as $maBC) {
						if ($maBC->getID() == $ma->getID()) { 
							echo($maBC->getTitle());
						} else {
							echo('<a href="media.php?id=' . $maBC->getID() . '">' . $maBC->getTitle() . '&nbsp;&#62;&nbsp;</a>');
						}
					}
				}
			 if ($ma->error) { ?>
			<br><span class="error">
				<?=$ma->error?>
			</span>
			<? } ?>
		</div>
		
		<? if (db::isError($res)) { 
			$res->outputList();
		} ?>
		
		<h1>area:</h1>
		<div class="inset">
		<h2><?= $ma->getTitle() ?></h2>
		<span class="small">
			Status: <strong><? if ($ma->isActive()) { ?>Active<? } else { ?>Inactive<? } ?></strong> -
			General Media: <strong><? if ($ma->isGeneralMedia()) { ?>Yes<? } else { ?>No<? } ?></strong>
		</span>
		<p>
		<?= $ma->getDescription() ?>
		</p>
		<? if (User::isAdmin()) { ?>
			<input class="button" type="button" value="Edit/Move" name="" onclick="location.href='media_area_edit.php?id=<?=$id?>'; return false">
		
			<? if ($ma->isActive()) { ?>
				<input class="button" type="button" value="Deactivate" name="" onclick="location.href='media.php?id=<?=$id?>&task=deactivate'; return false">
			<? } else { ?>
				<input class="button" type="button" value="Activate" name="" onclick="location.href='media.php?id=<?=$id?>&task=activate'; return false">
			<? } ?>
			<input class="button" type="button" value="Remove" name="" onclick="if (confirm('This will remove the area, and all media inside it. Continue?')) { location.href = '<?=$_SERVER['PHP_SELF']?>?task=delete&id=<?=$id?>';}">
		<? } ?>
		</div>
		
		<?	
			$editURL = "media_edit.php?id={$_GET['id']}&amp;";
			if (User::isAdmin()) { 
				$canEdit = true;
				$addURL = "media_add.php?id=" . $_GET['id'];
				
			}
			if ($ma->getDisplay() == 'grid') {
				include('layout/media_grid.php'); 
			} else {
				include('layout/media_list.php');
			}
		?>
		
		<h1>sub-areas:</h1>
		<div class="inset">
		<? if (User::isAdmin()) { ?>
		
		<a href="media_area_add.php?id=<?=$ma->getID()?>">Add Area</a>
		<? }?>
		
		
		<?
		
		$mas = $ma->getGeneralAreas();
		if ($mas) { ?>
		<ul>
		<? foreach($mas as $a) { ?>
			<li><b><a href="<?=$PHP_SELF?>?id=<?=$a->getID()?>"><?=$a->getTitle()?></a></b></li>
		<? } ?>
		</ul>
		<? } else {
			echo('<blockquote>None</blockquote>');
		} ?>
		
		</div>
	
	<? } else { 
	
		Error::outputDialog('Return to Media','media.php', 'This is not a general media area.');

	} ?>

<? } else if ($id == 0) { ?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="media.php">Media&nbsp;&#62;</a>
</div>

<? if (db::isError($res)) { 
	$res->outputList();
} ?>

<h1>sub-areas:</h1>
<div class="inset">
<? if (User::isAdmin()) { ?>
<a href="media_area_add.php">Add Area</a>
<? } ?>
<?

$mas = MediaArea::getGeneralAreas();
if ($mas) { ?>
<ul>
<? foreach($mas as $a) { ?>
	<li><b><a href="<?=$PHP_SELF?>?id=<?=$a->getID()?>"><?=$a->getTitle()?></a></b></li>
<? } ?>
</ul>
<? } else {
	echo('<blockquote>None</blockquote>');
} ?>


</div>

<? } else { ?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="media.php">Media&nbsp;&#62;</a>&nbsp;Invalid Entry
</div>

	<?=$ma->outputDialog('Return to Media','media.php');?>

<? } ?>

<? include('layout/footer.php'); ?>
